package com.bf.electroplating.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bf.electroplating.pojo.dto.IdDTO;
import com.bf.electroplating.pojo.dto.problem.ControlProblemFindingAddDTO;
import com.bf.electroplating.pojo.dto.problem.ControlProblemFindingQueryDTO;
import com.bf.electroplating.pojo.dto.problem.ControlProblemFindingUpdateDTO;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.problem.ControlProblemFindingVO;
import com.bf.electroplating.service.ControlProblemFindingService;
import com.bf.electroplating.util.easyexcel.excel.problem.ProblemFindingExcel;
import com.bf.electroplating.util.easyexcel.listener.ProblemFindingExcelListener;
import com.bf.electroplating.util.easyexcel.utils.ProblemImageUtils;
import com.bf.electroplating.util.idempotent.Idempotent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Tag(name = "问题发现管理")
@RestController
@RequestMapping("/controlProblemFinding")
public class ControlProblemFindingController {

    @Resource
    private ControlProblemFindingService controlProblemFindingService;
    @Resource
    private ProblemImageUtils problemImageUtils;
    @Autowired
    private ProblemFindingExcelListener problemFindingExcelListener;

    @Idempotent
    @Operation(summary = "新增问题发现")
    @PostMapping("/save")
    public ResultVO<Void> save(@Validated @RequestBody ControlProblemFindingAddDTO dto) {
        if (CollectionUtils.isEmpty(dto.getPhotoList())){
            return ResultVO.fail("请上传图片");
        }
        controlProblemFindingService.save(dto);
        return ResultVO.ok(null,"保存成功");
    }

    @Idempotent
    @Operation(summary = "更新问题发现")
    @PostMapping("/update")
    public ResultVO<Void> update(@Validated @RequestBody ControlProblemFindingUpdateDTO dto) {
        controlProblemFindingService.update(dto);
        return ResultVO.ok(null,"编辑成功");
    }

    @Operation(summary = "删除问题发现")
    @PostMapping("/delete")
    public ResultVO<Void> delete(@RequestBody IdDTO id) {
        controlProblemFindingService.delete(id.getId());
        return ResultVO.ok(null,"删除成功");
    }

    @Operation(summary = "批量删除问题发现")
    @PostMapping("/batchDelete")
    public ResultVO<Void> batchDelete(@RequestBody List<String> ids) {
        controlProblemFindingService.batchDelete(ids);
        return ResultVO.ok(null,"批量删除成功");
    }

    @Operation(summary = "获取问题发现详情")
    @PostMapping("/detail")
    public ResultVO<ControlProblemFindingVO> detail(@RequestBody IdDTO id) {
        return ResultVO.ok(controlProblemFindingService.detail(id.getId()));
    }

    @Operation(summary = "分页查询问题发现")
    @PostMapping("/page")
    public ResultVO<Page<ControlProblemFindingVO>> page(@RequestBody ControlProblemFindingQueryDTO dto) {
        return ResultVO.ok(controlProblemFindingService.page(dto));
    }

    @PostMapping(value = "/importData")
    @ResponseBody
    @Operation(summary = "导入")
    public ResultVO<Void> importData(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, List<String>> listMap = problemImageUtils.imageProcess(file);
            problemFindingExcelListener.setImageMap(listMap);
            EasyExcel.read(file.getInputStream(), ProblemFindingExcel.class, problemFindingExcelListener).sheet()
                    .headRowNumber(2) // 从第1行开始读取数据，0开始
                    .doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResultVO.ok("导入成功");
    }
} 